package com.testdocs.server.signin;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.testdocs.client.signin.UserInfo;
import com.testdocs.client.signin.SignInService;

@SuppressWarnings("serial")
public class SignInServiceImpl extends RemoteServiceServlet implements
		SignInService {

	public UserInfo signIn(String requestUri, String afterLogOutUri) {
		UserService userService = UserServiceFactory.getUserService();
		User user = userService.getCurrentUser();
		UserInfo loginInfo = new UserInfo();

		if (user != null) {
			loginInfo.setSignedIn(true);
			loginInfo.setEmailAddress(user.getEmail());
			loginInfo.setNickname(user.getNickname());
			loginInfo.setSignOutUrl(userService.createLogoutURL(afterLogOutUri));
		} else {
			loginInfo.setSignedIn(false);
			loginInfo.setSignInUrl(userService.createLoginURL(requestUri));
		}
		return loginInfo;
	}

}